<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mockclock.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_mockclock.js.html">mockclock.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Mock Clock implementation for working with setTimeout,
<a name="line17"></a> * setInterval, clearTimeout and clearInterval within unit tests.
<a name="line18"></a> *
<a name="line19"></a> * Derived from jsUnitMockTimeout.js, contributed to JsUnit by
<a name="line20"></a> * Pivotal Computer Systems, www.pivotalsf.com
<a name="line21"></a> *
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.testing.MockClock&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.events&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.events.Event&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Class for unit testing code that uses setTimeout and clearTimeout.
<a name="line35"></a> *
<a name="line36"></a> * NOTE: If you are using MockClock to test code that makes use of
<a name="line37"></a> *       goog.fx.Animation, then you must either:
<a name="line38"></a> *
<a name="line39"></a> * 1. Install and dispose of the MockClock in setUpPage() and tearDownPage()
<a name="line40"></a> *    respectively (rather than setUp()/tearDown()).
<a name="line41"></a> *
<a name="line42"></a> * or
<a name="line43"></a> *
<a name="line44"></a> * 2. Ensure that every test clears the animation queue by calling
<a name="line45"></a> *    mockClock.tick(x) at the end of each test function (where `x` is large
<a name="line46"></a> *    enough to complete all animations).
<a name="line47"></a> *
<a name="line48"></a> * Otherwise, if any animation is left pending at the time that
<a name="line49"></a> * MockClock.dispose() is called, that will permanently prevent any future
<a name="line50"></a> * animations from playing on the page.
<a name="line51"></a> *
<a name="line52"></a> * @param {boolean=} opt_autoInstall Install the MockClock at construction time.
<a name="line53"></a> * @constructor
<a name="line54"></a> * @extends {goog.Disposable}
<a name="line55"></a> */
<a name="line56"></a>goog.testing.MockClock = function(opt_autoInstall) {
<a name="line57"></a>  goog.Disposable.call(this);
<a name="line58"></a>
<a name="line59"></a>  /**
<a name="line60"></a>   * Reverse-order queue of timers to fire.
<a name="line61"></a>   *
<a name="line62"></a>   * The last item of the queue is popped off.  Insertion happens from the
<a name="line63"></a>   * right.  For example, the expiration times for each element of the queue
<a name="line64"></a>   * might be in the order 300, 200, 200.
<a name="line65"></a>   *
<a name="line66"></a>   * @type {Array.&lt;Object&gt;}
<a name="line67"></a>   * @private
<a name="line68"></a>   */
<a name="line69"></a>  this.queue_ = [];
<a name="line70"></a>
<a name="line71"></a>  /**
<a name="line72"></a>   * Set of timeouts that should be treated as cancelled.
<a name="line73"></a>   *
<a name="line74"></a>   * Rather than removing cancelled timers directly from the queue, this set
<a name="line75"></a>   * simply marks them as deleted so that they can be ignored when their
<a name="line76"></a>   * turn comes up.  The keys are the timeout keys that are cancelled, each
<a name="line77"></a>   * mapping to true.
<a name="line78"></a>   *
<a name="line79"></a>   * @type {Object}
<a name="line80"></a>   * @private
<a name="line81"></a>   */
<a name="line82"></a>  this.deletedKeys_ = {};
<a name="line83"></a>
<a name="line84"></a>  if (opt_autoInstall) {
<a name="line85"></a>    this.install();
<a name="line86"></a>  }
<a name="line87"></a>};
<a name="line88"></a>goog.inherits(goog.testing.MockClock, goog.Disposable);
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * Default wait timeout for mocking requestAnimationFrame (in milliseconds).
<a name="line93"></a> *
<a name="line94"></a> * @type {number}
<a name="line95"></a> * @const
<a name="line96"></a> */
<a name="line97"></a>goog.testing.MockClock.REQUEST_ANIMATION_FRAME_TIMEOUT = 20;
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * Count of the number of timeouts made.
<a name="line102"></a> * @type {number}
<a name="line103"></a> * @private
<a name="line104"></a> */
<a name="line105"></a>goog.testing.MockClock.prototype.timeoutsMade_ = 0;
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * PropertyReplacer instance which overwrites and resets setTimeout,
<a name="line110"></a> * setInterval, etc. or null if the MockClock is not installed.
<a name="line111"></a> * @type {goog.testing.PropertyReplacer}
<a name="line112"></a> * @private
<a name="line113"></a> */
<a name="line114"></a>goog.testing.MockClock.prototype.replacer_ = null;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Map of deleted keys.  These keys represents keys that were deleted in a
<a name="line119"></a> * clearInterval, timeoutid -&gt; object.
<a name="line120"></a> * @type {Object}
<a name="line121"></a> * @private
<a name="line122"></a> */
<a name="line123"></a>goog.testing.MockClock.prototype.deletedKeys_ = null;
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * The current simulated time in milliseconds.
<a name="line128"></a> * @type {number}
<a name="line129"></a> * @private
<a name="line130"></a> */
<a name="line131"></a>goog.testing.MockClock.prototype.nowMillis_ = 0;
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Additional delay between the time a timeout was set to fire, and the time
<a name="line136"></a> * it actually fires.  Useful for testing workarounds for this Firefox 2 bug:
<a name="line137"></a> * https://bugzilla.mozilla.org/show_bug.cgi?id=291386
<a name="line138"></a> * May be negative.
<a name="line139"></a> * @type {number}
<a name="line140"></a> * @private
<a name="line141"></a> */
<a name="line142"></a>goog.testing.MockClock.prototype.timeoutDelay_ = 0;
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Installs the MockClock by overriding the global object&#39;s implementation of
<a name="line147"></a> * setTimeout, setInterval, clearTimeout and clearInterval.
<a name="line148"></a> */
<a name="line149"></a>goog.testing.MockClock.prototype.install = function() {
<a name="line150"></a>  if (!this.replacer_) {
<a name="line151"></a>    var r = this.replacer_ = new goog.testing.PropertyReplacer();
<a name="line152"></a>    r.set(goog.global, &#39;setTimeout&#39;, goog.bind(this.setTimeout_, this));
<a name="line153"></a>    r.set(goog.global, &#39;setInterval&#39;, goog.bind(this.setInterval_, this));
<a name="line154"></a>    r.set(goog.global, &#39;clearTimeout&#39;, goog.bind(this.clearTimeout_, this));
<a name="line155"></a>    r.set(goog.global, &#39;clearInterval&#39;, goog.bind(this.clearInterval_, this));
<a name="line156"></a>
<a name="line157"></a>    // Replace the requestAnimationFrame functions.
<a name="line158"></a>    this.replaceRequestAnimationFrame_();
<a name="line159"></a>
<a name="line160"></a>    // PropertyReplacer#set can&#39;t be called with renameable functions.
<a name="line161"></a>    this.oldGoogNow_ = goog.now;
<a name="line162"></a>    goog.now = goog.bind(this.getCurrentTime, this);
<a name="line163"></a>  }
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Installs the mocks for requestAnimationFrame and cancelRequestAnimationFrame.
<a name="line169"></a> * @private
<a name="line170"></a> */
<a name="line171"></a>goog.testing.MockClock.prototype.replaceRequestAnimationFrame_ = function() {
<a name="line172"></a>  var r = this.replacer_;
<a name="line173"></a>  var requestFuncs = [&#39;requestAnimationFrame&#39;,
<a name="line174"></a>                      &#39;webkitRequestAnimationFrame&#39;,
<a name="line175"></a>                      &#39;mozRequestAnimationFrame&#39;,
<a name="line176"></a>                      &#39;oRequestAnimationFrame&#39;,
<a name="line177"></a>                      &#39;msRequestAnimationFrame&#39;];
<a name="line178"></a>
<a name="line179"></a>  var cancelFuncs = [&#39;cancelRequestAnimationFrame&#39;,
<a name="line180"></a>                     &#39;webkitCancelRequestAnimationFrame&#39;,
<a name="line181"></a>                     &#39;mozCancelRequestAnimationFrame&#39;,
<a name="line182"></a>                     &#39;oCancelRequestAnimationFrame&#39;,
<a name="line183"></a>                     &#39;msCancelRequestAnimationFrame&#39;];
<a name="line184"></a>
<a name="line185"></a>  for (var i = 0; i &lt; requestFuncs.length; ++i) {
<a name="line186"></a>    if (goog.global &amp;&amp; goog.global[requestFuncs[i]]) {
<a name="line187"></a>      r.set(goog.global, requestFuncs[i],
<a name="line188"></a>          goog.bind(this.requestAnimationFrame_, this));
<a name="line189"></a>    }
<a name="line190"></a>  }
<a name="line191"></a>
<a name="line192"></a>  for (var i = 0; i &lt; cancelFuncs.length; ++i) {
<a name="line193"></a>    if (goog.global &amp;&amp; goog.global[cancelFuncs[i]]) {
<a name="line194"></a>      r.set(goog.global, cancelFuncs[i],
<a name="line195"></a>          goog.bind(this.cancelRequestAnimationFrame_, this));
<a name="line196"></a>    }
<a name="line197"></a>  }
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/**
<a name="line202"></a> * Removes the MockClock&#39;s hooks into the global object&#39;s functions and revert
<a name="line203"></a> * to their original values.
<a name="line204"></a> */
<a name="line205"></a>goog.testing.MockClock.prototype.uninstall = function() {
<a name="line206"></a>  if (this.replacer_) {
<a name="line207"></a>    this.replacer_.reset();
<a name="line208"></a>    this.replacer_ = null;
<a name="line209"></a>    goog.now = this.oldGoogNow_;
<a name="line210"></a>  }
<a name="line211"></a>};
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/** @override */
<a name="line215"></a>goog.testing.MockClock.prototype.disposeInternal = function() {
<a name="line216"></a>  this.uninstall();
<a name="line217"></a>  this.queue_ = null;
<a name="line218"></a>  this.deletedKeys_ = null;
<a name="line219"></a>  goog.testing.MockClock.superClass_.disposeInternal.call(this);
<a name="line220"></a>};
<a name="line221"></a>
<a name="line222"></a>
<a name="line223"></a>/**
<a name="line224"></a> * Resets the MockClock, removing all timeouts that are scheduled and resets
<a name="line225"></a> * the fake timer count.
<a name="line226"></a> */
<a name="line227"></a>goog.testing.MockClock.prototype.reset = function() {
<a name="line228"></a>  this.queue_ = [];
<a name="line229"></a>  this.deletedKeys_ = {};
<a name="line230"></a>  this.nowMillis_ = 0;
<a name="line231"></a>  this.timeoutsMade_ = 0;
<a name="line232"></a>  this.timeoutDelay_ = 0;
<a name="line233"></a>};
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>/**
<a name="line237"></a> * Sets the amount of time between when a timeout is scheduled to fire and when
<a name="line238"></a> * it actually fires.
<a name="line239"></a> * @param {number} delay The delay in milliseconds.  May be negative.
<a name="line240"></a> */
<a name="line241"></a>goog.testing.MockClock.prototype.setTimeoutDelay = function(delay) {
<a name="line242"></a>  this.timeoutDelay_ = delay;
<a name="line243"></a>};
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>/**
<a name="line247"></a> * @return {number} delay The amount of time between when a timeout is
<a name="line248"></a> *     scheduled to fire and when it actually fires, in milliseconds.  May
<a name="line249"></a> *     be negative.
<a name="line250"></a> */
<a name="line251"></a>goog.testing.MockClock.prototype.getTimeoutDelay = function() {
<a name="line252"></a>  return this.timeoutDelay_;
<a name="line253"></a>};
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * Increments the MockClock&#39;s time by a given number of milliseconds, running
<a name="line258"></a> * any functions that are now overdue.
<a name="line259"></a> * @param {number=} opt_millis Number of milliseconds to increment the counter.
<a name="line260"></a> *     If not specified, clock ticks 1 millisecond.
<a name="line261"></a> * @return {number} Current mock time in milliseconds.
<a name="line262"></a> */
<a name="line263"></a>goog.testing.MockClock.prototype.tick = function(opt_millis) {
<a name="line264"></a>  if (typeof opt_millis != &#39;number&#39;) {
<a name="line265"></a>    opt_millis = 1;
<a name="line266"></a>  }
<a name="line267"></a>  var endTime = this.nowMillis_ + opt_millis;
<a name="line268"></a>  this.runFunctionsWithinRange_(endTime);
<a name="line269"></a>  this.nowMillis_ = endTime;
<a name="line270"></a>  return endTime;
<a name="line271"></a>};
<a name="line272"></a>
<a name="line273"></a>
<a name="line274"></a>/**
<a name="line275"></a> * @return {number} The number of timeouts that have been scheduled.
<a name="line276"></a> */
<a name="line277"></a>goog.testing.MockClock.prototype.getTimeoutsMade = function() {
<a name="line278"></a>  return this.timeoutsMade_;
<a name="line279"></a>};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * @return {number} The MockClock&#39;s current time in milliseconds.
<a name="line284"></a> */
<a name="line285"></a>goog.testing.MockClock.prototype.getCurrentTime = function() {
<a name="line286"></a>  return this.nowMillis_;
<a name="line287"></a>};
<a name="line288"></a>
<a name="line289"></a>
<a name="line290"></a>/**
<a name="line291"></a> * @param {number} timeoutKey The timeout key.
<a name="line292"></a> * @return {boolean} Whether the timer has been set and not cleared,
<a name="line293"></a> *     independent of the timeout&#39;s expiration.  In other words, the timeout
<a name="line294"></a> *     could have passed or could be scheduled for the future.  Either way,
<a name="line295"></a> *     this function returns true or false depending only on whether the
<a name="line296"></a> *     provided timeoutKey represents a timeout that has been set and not
<a name="line297"></a> *     cleared.
<a name="line298"></a> */
<a name="line299"></a>goog.testing.MockClock.prototype.isTimeoutSet = function(timeoutKey) {
<a name="line300"></a>  return timeoutKey &lt;= this.timeoutsMade_ &amp;&amp; !this.deletedKeys_[timeoutKey];
<a name="line301"></a>};
<a name="line302"></a>
<a name="line303"></a>
<a name="line304"></a>/**
<a name="line305"></a> * Runs any function that is scheduled before a certain time.  Timeouts can
<a name="line306"></a> * be made to fire early or late if timeoutDelay_ is non-0.
<a name="line307"></a> * @param {number} endTime The latest time in the range, in milliseconds.
<a name="line308"></a> * @private
<a name="line309"></a> */
<a name="line310"></a>goog.testing.MockClock.prototype.runFunctionsWithinRange_ = function(
<a name="line311"></a>    endTime) {
<a name="line312"></a>  var adjustedEndTime = endTime - this.timeoutDelay_;
<a name="line313"></a>
<a name="line314"></a>  // Repeatedly pop off the last item since the queue is always sorted.
<a name="line315"></a>  while (this.queue_.length &amp;&amp;
<a name="line316"></a>      this.queue_[this.queue_.length - 1].runAtMillis &lt;= adjustedEndTime) {
<a name="line317"></a>    var timeout = this.queue_.pop();
<a name="line318"></a>
<a name="line319"></a>    if (!(timeout.timeoutKey in this.deletedKeys_)) {
<a name="line320"></a>      // Only move time forwards.
<a name="line321"></a>      this.nowMillis_ = Math.max(this.nowMillis_,
<a name="line322"></a>          timeout.runAtMillis + this.timeoutDelay_);
<a name="line323"></a>      // Call timeout in global scope and pass the timeout key as the argument.
<a name="line324"></a>      timeout.funcToCall.call(goog.global, timeout.timeoutKey);
<a name="line325"></a>      // In case the interval was cleared in the funcToCall
<a name="line326"></a>      if (timeout.recurring) {
<a name="line327"></a>        this.scheduleFunction_(
<a name="line328"></a>            timeout.timeoutKey, timeout.funcToCall, timeout.millis, true);
<a name="line329"></a>      }
<a name="line330"></a>    }
<a name="line331"></a>  }
<a name="line332"></a>};
<a name="line333"></a>
<a name="line334"></a>
<a name="line335"></a>/**
<a name="line336"></a> * Schedules a function to be run at a certain time.
<a name="line337"></a> * @param {number} timeoutKey The timeout key.
<a name="line338"></a> * @param {Function} funcToCall The function to call.
<a name="line339"></a> * @param {number} millis The number of milliseconds to call it in.
<a name="line340"></a> * @param {boolean} recurring Whether to function call should recur.
<a name="line341"></a> * @private
<a name="line342"></a> */
<a name="line343"></a>goog.testing.MockClock.prototype.scheduleFunction_ = function(
<a name="line344"></a>    timeoutKey, funcToCall, millis, recurring) {
<a name="line345"></a>  var timeout = {
<a name="line346"></a>    runAtMillis: this.nowMillis_ + millis,
<a name="line347"></a>    funcToCall: funcToCall,
<a name="line348"></a>    recurring: recurring,
<a name="line349"></a>    timeoutKey: timeoutKey,
<a name="line350"></a>    millis: millis
<a name="line351"></a>  };
<a name="line352"></a>
<a name="line353"></a>  goog.testing.MockClock.insert_(timeout, this.queue_);
<a name="line354"></a>};
<a name="line355"></a>
<a name="line356"></a>
<a name="line357"></a>/**
<a name="line358"></a> * Inserts a timer descriptor into a descending-order queue.
<a name="line359"></a> *
<a name="line360"></a> * Later-inserted duplicates appear at lower indices.  For example, the
<a name="line361"></a> * asterisk in (5,4,*,3,2,1) would be the insertion point for 3.
<a name="line362"></a> *
<a name="line363"></a> * @param {Object} timeout The timeout to insert, with numerical runAtMillis
<a name="line364"></a> *     property.
<a name="line365"></a> * @param {Array.&lt;Object&gt;} queue The queue to insert into, with each element
<a name="line366"></a> *     having a numerical runAtMillis property.
<a name="line367"></a> * @private
<a name="line368"></a> */
<a name="line369"></a>goog.testing.MockClock.insert_ = function(timeout, queue) {
<a name="line370"></a>  // Although insertion of N items is quadratic, requiring goog.structs.Heap
<a name="line371"></a>  // from a unit test will make tests more prone to breakage.  Since unit
<a name="line372"></a>  // tests are normally small, scalability is not a primary issue.
<a name="line373"></a>
<a name="line374"></a>  // Find an insertion point.  Since the queue is in reverse order (so we
<a name="line375"></a>  // can pop rather than unshift), and later timers with the same time stamp
<a name="line376"></a>  // should be executed later, we look for the element strictly greater than
<a name="line377"></a>  // the one we are inserting.
<a name="line378"></a>
<a name="line379"></a>  for (var i = queue.length; i != 0; i--) {
<a name="line380"></a>    if (queue[i - 1].runAtMillis &gt; timeout.runAtMillis) {
<a name="line381"></a>      break;
<a name="line382"></a>    }
<a name="line383"></a>    queue[i] = queue[i - 1];
<a name="line384"></a>  }
<a name="line385"></a>
<a name="line386"></a>  queue[i] = timeout;
<a name="line387"></a>};
<a name="line388"></a>
<a name="line389"></a>
<a name="line390"></a>/**
<a name="line391"></a> * Maximum 32-bit signed integer.
<a name="line392"></a> *
<a name="line393"></a> * Timeouts over this time return immediately in many browsers, due to integer
<a name="line394"></a> * overflow.  Such known browsers include Firefox, Chrome, and Safari, but not
<a name="line395"></a> * IE.
<a name="line396"></a> *
<a name="line397"></a> * @type {number}
<a name="line398"></a> * @private
<a name="line399"></a> */
<a name="line400"></a>goog.testing.MockClock.MAX_INT_ = 2147483647;
<a name="line401"></a>
<a name="line402"></a>
<a name="line403"></a>/**
<a name="line404"></a> * Schedules a function to be called after {@code millis} milliseconds.
<a name="line405"></a> * Mock implementation for setTimeout.
<a name="line406"></a> * @param {Function} funcToCall The function to call.
<a name="line407"></a> * @param {number} millis The number of milliseconds to call it after.
<a name="line408"></a> * @return {number} The number of timeouts created.
<a name="line409"></a> * @private
<a name="line410"></a> */
<a name="line411"></a>goog.testing.MockClock.prototype.setTimeout_ = function(funcToCall, millis) {
<a name="line412"></a>  if (millis &gt; goog.testing.MockClock.MAX_INT_) {
<a name="line413"></a>    throw Error(
<a name="line414"></a>        &#39;Bad timeout value: &#39; + millis + &#39;.  Timeouts over MAX_INT &#39; +
<a name="line415"></a>        &#39;(24.8 days) cause timeouts to be fired &#39; +
<a name="line416"></a>        &#39;immediately in most browsers, except for IE.&#39;);
<a name="line417"></a>  }
<a name="line418"></a>  this.timeoutsMade_ = this.timeoutsMade_ + 1;
<a name="line419"></a>  this.scheduleFunction_(this.timeoutsMade_, funcToCall, millis, false);
<a name="line420"></a>  return this.timeoutsMade_;
<a name="line421"></a>};
<a name="line422"></a>
<a name="line423"></a>
<a name="line424"></a>/**
<a name="line425"></a> * Schedules a function to be called every {@code millis} milliseconds.
<a name="line426"></a> * Mock implementation for setInterval.
<a name="line427"></a> * @param {Function} funcToCall The function to call.
<a name="line428"></a> * @param {number} millis The number of milliseconds between calls.
<a name="line429"></a> * @return {number} The number of timeouts created.
<a name="line430"></a> * @private
<a name="line431"></a> */
<a name="line432"></a>goog.testing.MockClock.prototype.setInterval_ = function(funcToCall, millis) {
<a name="line433"></a>  this.timeoutsMade_ = this.timeoutsMade_ + 1;
<a name="line434"></a>  this.scheduleFunction_(this.timeoutsMade_, funcToCall, millis, true);
<a name="line435"></a>  return this.timeoutsMade_;
<a name="line436"></a>};
<a name="line437"></a>
<a name="line438"></a>
<a name="line439"></a>/**
<a name="line440"></a> * Schedules a function to be called when an animation frame is triggered.
<a name="line441"></a> * Mock implementation for requestAnimationFrame.
<a name="line442"></a> * @param {Function} funcToCall The function to call.
<a name="line443"></a> * @return {number} The number of timeouts created.
<a name="line444"></a> * @private
<a name="line445"></a> */
<a name="line446"></a>goog.testing.MockClock.prototype.requestAnimationFrame_ = function(funcToCall) {
<a name="line447"></a>  return this.setTimeout_(goog.bind(function() {
<a name="line448"></a>    if (funcToCall) {
<a name="line449"></a>      funcToCall(this.getCurrentTime());
<a name="line450"></a>    } else if (goog.global.mozRequestAnimationFrame) {
<a name="line451"></a>      var event = new goog.testing.events.Event(&#39;MozBeforePaint&#39;, goog.global);
<a name="line452"></a>      event[&#39;timeStamp&#39;] = this.getCurrentTime();
<a name="line453"></a>      goog.testing.events.fireBrowserEvent(event);
<a name="line454"></a>    }
<a name="line455"></a>  }, this), goog.testing.MockClock.REQUEST_ANIMATION_FRAME_TIMEOUT);
<a name="line456"></a>};
<a name="line457"></a>
<a name="line458"></a>
<a name="line459"></a>/**
<a name="line460"></a> * Clears a timeout.
<a name="line461"></a> * Mock implementation for clearTimeout.
<a name="line462"></a> * @param {number} timeoutKey The timeout key to clear.
<a name="line463"></a> * @private
<a name="line464"></a> */
<a name="line465"></a>goog.testing.MockClock.prototype.clearTimeout_ = function(timeoutKey) {
<a name="line466"></a>  // Some common libraries register static state with timers.
<a name="line467"></a>  // This is bad. It leads to all sorts of crazy test problems where
<a name="line468"></a>  // 1) Test A sets up a new mock clock and a static timer.
<a name="line469"></a>  // 2) Test B sets up a new mock clock, but re-uses the static timer
<a name="line470"></a>  //    from Test A.
<a name="line471"></a>  // 3) A timeout key from test A gets cleared, breaking a timeout in
<a name="line472"></a>  //    Test B.
<a name="line473"></a>  //
<a name="line474"></a>  // For now, we just hackily fail silently if someone tries to clear a timeout
<a name="line475"></a>  // key before we&#39;ve allocated it.
<a name="line476"></a>  // Ideally, we should throw an exception if we see this happening.
<a name="line477"></a>  //
<a name="line478"></a>  // TODO(user): We might also try allocating timeout ids from a global
<a name="line479"></a>  // pool rather than a local pool.
<a name="line480"></a>  if (this.isTimeoutSet(timeoutKey)) {
<a name="line481"></a>    this.deletedKeys_[timeoutKey] = true;
<a name="line482"></a>  }
<a name="line483"></a>};
<a name="line484"></a>
<a name="line485"></a>
<a name="line486"></a>/**
<a name="line487"></a> * Clears an interval.
<a name="line488"></a> * Mock implementation for clearInterval.
<a name="line489"></a> * @param {number} timeoutKey The interval key to clear.
<a name="line490"></a> * @private
<a name="line491"></a> */
<a name="line492"></a>goog.testing.MockClock.prototype.clearInterval_ = function(timeoutKey) {
<a name="line493"></a>  this.clearTimeout_(timeoutKey);
<a name="line494"></a>};
<a name="line495"></a>
<a name="line496"></a>
<a name="line497"></a>/**
<a name="line498"></a> * Clears a requestAnimationFrame.
<a name="line499"></a> * Mock implementation for cancelRequestAnimationFrame.
<a name="line500"></a> * @param {number} timeoutKey The requestAnimationFrame key to clear.
<a name="line501"></a> * @private
<a name="line502"></a> */
<a name="line503"></a>goog.testing.MockClock.prototype.cancelRequestAnimationFrame_ =
<a name="line504"></a>    function(timeoutKey) {
<a name="line505"></a>  this.clearTimeout_(timeoutKey);
<a name="line506"></a>};
</pre>


</body>
</html>
